# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=godot
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=3.2.3
pkgrel=1
pkgdesc='An advanced, feature packed, multi-platform 2D and 3D game engine (mingw-w64)'
arch=('any')
url="http://www.godotengine.org/"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-yasm" "${MINGW_PACKAGE_PREFIX}-scons")
options=('!emptydirs' 'staticlibs' 'strip')
source=("https://github.com/godotengine/godot/archive/${pkgver}-stable.tar.gz")
sha256sums=('4c2a8e7da1ad05c6223b0ff6cf2be124dad6708b56a8ec9910dc2aaf82a553ae')

prepare() {
  cd "$srcdir"

}

build() {
  msg "Build for ${CARCH}"

  rm -rf build-${CARCH} | true
  cp -r "${_realname}-${pkgver}-stable" "build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  declare -a extra_config
  if [ "$CARCH" = "x86_64" ]; then
    extra_config+=("use_lto=yes")
    extra_config+=("bits=64")
  else
    # lto is broken on 32bit
    extra_config+=("use_lto=no")
    extra_config+=("bits=32")
  fi

  # TODO: try to use some system libs:
  # https://github.com/msys2/MINGW-packages/pull/7212#issuecomment-723002506
  ${MINGW_PREFIX}/bin/scons ${MAKEFLAGS} \
    platform=windows \
    target=release_debug \
    use_mingw=yes \
    "${extra_config[@]}" \
    tools=yes
}

package() {
  cd "${srcdir}/build-${CARCH}"
  install -Dm755 bin/godot.windows.opt.tools.*.exe "${pkgdir}${MINGW_PREFIX}/bin/godot.exe"
  install -Dm644 icon.svg "${pkgdir}${MINGW_PREFIX}/share/pixmaps/godot.svg"
  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/godot/LICENSE.txt"
  install -D -m644 "misc/dist/linux/godot.6" "${pkgdir}${MINGW_PREFIX}/share/man/man6/godot.6"
}